//
// Created by Administrator on 2025/9/13.
//

#include "bsp_callbacks.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "nanomodbus_config.h"
#include "sdcard.h"
extern SemaphoreHandle_t rtu_tx_sem;
extern xSemaphoreHandle logger_semaphor;

extern SD_HandleTypeDef g_sd_handle;
static DMA_HandleTypeDef g_sd_dma_tx_handle;
static DMA_HandleTypeDef g_sd_dma_rx_handle;
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance==MB_UART.Instance)
    {
        BaseType_t xHigherPriorityTaskWoken=pdFALSE;
        xSemaphoreGiveFromISR(rtu_tx_sem, &xHigherPriorityTaskWoken);
        portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
    }
    else if (huart->Instance == USART1)
    {
        BaseType_t xHigherPriorityTaskWoken = pdFALSE;
        if (logger_semaphor != NULL)
        {
            xSemaphoreGiveFromISR(logger_semaphor, &xHigherPriorityTaskWoken);
        }
        portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
    }
}


void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
{
    if (hsd->Instance==SDIO)
    {
        GPIO_InitTypeDef sdio_gpio_initstruct={0};
        __HAL_RCC_SDIO_CLK_ENABLE();
        SDCARD_SDIO_CLK_GPIO_CLK_ENABLE();
        SDCARD_SDIO_CMD_GPIO_CLK_ENABLE();
        SDCARD_SDIO_D0_GPIO_CLK_ENABLE();
        SDCARD_SDIO_D1_GPIO_CLK_ENABLE();
        SDCARD_SDIO_D2_GPIO_CLK_ENABLE();
        SDCARD_SDIO_D3_GPIO_CLK_ENABLE();

        sdio_gpio_initstruct.Pin=SDCARD_SDIO_D0_PIN;
        sdio_gpio_initstruct.Mode=GPIO_MODE_AF_PP;
        sdio_gpio_initstruct.Pull=GPIO_PULLUP;
        sdio_gpio_initstruct.Speed=GPIO_SPEED_FREQ_HIGH;
        sdio_gpio_initstruct.Alternate=GPIO_AF12_SDIO;
        HAL_GPIO_Init(SDCARD_SDIO_D0_PORT,&sdio_gpio_initstruct);

        sdio_gpio_initstruct.Pin=SDCARD_SDIO_D1_PIN;
        HAL_GPIO_Init(SDCARD_SDIO_D1_PORT,&sdio_gpio_initstruct);

        sdio_gpio_initstruct.Pin=SDCARD_SDIO_D2_PIN;
        HAL_GPIO_Init(SDCARD_SDIO_D2_PORT,&sdio_gpio_initstruct);

        sdio_gpio_initstruct.Pin=SDCARD_SDIO_D3_PIN;
        HAL_GPIO_Init(SDCARD_SDIO_D3_PORT,&sdio_gpio_initstruct);

        sdio_gpio_initstruct.Pin=SDCARD_SDIO_CLK_PIN;
        HAL_GPIO_Init(SDCARD_SDIO_CLK_PORT,&sdio_gpio_initstruct);

        sdio_gpio_initstruct.Pin=SDCARD_SDIO_CMD_PIN;
        HAL_GPIO_Init(SDCARD_SDIO_CMD_PORT,&sdio_gpio_initstruct);

        __HAL_RCC_DMA2_CLK_ENABLE();
        g_sd_dma_rx_handle.Instance=DMA2_Stream3;
        g_sd_dma_rx_handle.Init.Channel=DMA_CHANNEL_4;
        g_sd_dma_rx_handle.Init.Direction=DMA_PERIPH_TO_MEMORY;
        g_sd_dma_rx_handle.Init.PeriphInc=DMA_PINC_DISABLE;
        g_sd_dma_rx_handle.Init.MemInc=DMA_MINC_ENABLE;
        g_sd_dma_rx_handle.Init.PeriphDataAlignment=DMA_PDATAALIGN_WORD;
        g_sd_dma_rx_handle.Init.MemDataAlignment=DMA_PDATAALIGN_WORD;
        g_sd_dma_rx_handle.Init.Mode=DMA_PFCTRL;
        g_sd_dma_rx_handle.Init.Priority=DMA_PRIORITY_HIGH;
        g_sd_dma_rx_handle.Init.FIFOMode=DMA_FIFOMODE_ENABLE;
        g_sd_dma_rx_handle.Init.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL;
        g_sd_dma_rx_handle.Init.MemBurst=DMA_MBURST_INC4;
        g_sd_dma_rx_handle.Init.PeriphBurst=DMA_PBURST_INC4;
        HAL_DMA_Init(&g_sd_dma_rx_handle);

        __HAL_LINKDMA(hsd,hdmarx,g_sd_dma_rx_handle);

        g_sd_dma_tx_handle.Instance=DMA2_Stream6;
        g_sd_dma_tx_handle.Init.Channel=DMA_CHANNEL_4;
        g_sd_dma_tx_handle.Init.Direction=DMA_MEMORY_TO_PERIPH;
        g_sd_dma_tx_handle.Init.PeriphInc=DMA_PINC_DISABLE;
        g_sd_dma_tx_handle.Init.MemInc=DMA_MINC_ENABLE;
        g_sd_dma_tx_handle.Init.PeriphDataAlignment=DMA_PDATAALIGN_WORD;
        g_sd_dma_tx_handle.Init.MemDataAlignment=DMA_PDATAALIGN_WORD;
        g_sd_dma_tx_handle.Init.Mode=DMA_PFCTRL;
        g_sd_dma_tx_handle.Init.Priority=DMA_PRIORITY_HIGH;
        g_sd_dma_tx_handle.Init.FIFOMode=DMA_FIFOMODE_ENABLE;
        g_sd_dma_tx_handle.Init.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL;
        g_sd_dma_tx_handle.Init.MemBurst=DMA_MBURST_INC4;
        g_sd_dma_tx_handle.Init.PeriphBurst=DMA_PBURST_INC4;
        HAL_DMA_Init(&g_sd_dma_tx_handle);

        __HAL_LINKDMA(hsd,hdmatx,g_sd_dma_tx_handle);

        HAL_NVIC_SetPriority(SDIO_IRQn, 5, 0);
        HAL_NVIC_SetPriority(DMA2_Stream3_IRQn, 6, 0);
        HAL_NVIC_SetPriority(DMA2_Stream6_IRQn, 6, 0);
        HAL_NVIC_EnableIRQ(SDIO_IRQn);
        HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn);
        HAL_NVIC_EnableIRQ(DMA2_Stream6_IRQn);
    }
}


//SDIO中断部分
void SDIO_IRQHandler(void)
{
    HAL_SD_IRQHandler(&g_sd_handle);
}

void DMA2_Stream3_IRQHandler(void)
{
    HAL_DMA_IRQHandler(&g_sd_dma_rx_handle);
}


void DMA2_Stream6_IRQHandler(void)
{
    HAL_DMA_IRQHandler(&g_sd_dma_tx_handle);
}

extern void BSP_SD_DMA_READ_CB(void);

extern void BSP_SD_DMA_WRITE_CB(void);

void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
{
    BSP_SD_DMA_READ_CB();
    // if (hsd->Instance==SDIO)
    // {
    //     sdcard_rx_flag=true;
    // }
}

void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
{
    // if (hsd->Instance==SDIO)
    // {
    //     sdcard_tx_flag=true;
    // }
    BSP_SD_DMA_WRITE_CB();
}